package com.yuandian.ops.workbench.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.excel.util.DateUtils;
import com.yuandian.base.service.ITaskDataService;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.constant.Constants;
import com.yuandian.ops.workbench.po.AlarmListParams;
import com.yuandian.ops.workbench.po.StatusAlarmStatParams;
import com.yuandian.ops.workbench.service.IAreaAlarmAnalysisService;
import com.yuandian.ops.workbench.vo.*;
import com.yuandian.utils.BeanUtils;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.YDateUtil;
import com.yuandian.utils.poi.ExcelTitleUtils;
import com.yuandian.utils.poi.ExcelUtil;
import com.yuandian.vo.TrendDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 运维工作台 - 区域告警分析
 * @author linshihe
 * @date 2021/12/17
 */
@RestController
@RequestMapping("/ywgzt/areaAlarm")
@Api(tags = "区域告警分析-Controller")
public class AreaAlarmAnalysisController {

	@Autowired
	private ITaskDataService taskDataService;

	@Autowired
	private IAreaAlarmAnalysisService areaAlarmAnalysisService;


	/**
	 * 二级-首页中间地图-下钻-右边三个折线图
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "二级-首页中间地图-下钻-右边三个折线图", notes = "二级-首页中间地图-下钻-右边三个折线图")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/analysis/areaDetailTrend")
	public R<Object> areaDetailTrend(@RequestBody StatusAlarmStatParams params) {
		TrendDataVo trendDataVo = taskDataService.getTrendObject("运维工作台-业务告警", params.getStartTime(), params.getEndTime(), null);
		if (trendDataVo.isLastShow()){
			params.setEndTime(DateUtil.formatDateTime(new Date(DateUtil.parse(trendDataVo.getTrendEndTime()).getTime() + 60*1000)));
		}
		List<BusiStatusAlarmStatVo> list = areaAlarmAnalysisService.queryAreaTrend(params);
		return R.ok(list);
	}


	/**
	 * 二级-首页中间地图-下钻-告警数据折线图
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "二级-首页中间地图-下钻-告警数据折线图", notes = "二级-首页中间地图-下钻-告警数据折线图")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/analysis/alarmTrend")
	public R<Object> areaAlarmTrend(@RequestBody AlarmListParams params) {
		List<AlarmTrendVo> list = areaAlarmAnalysisService.queryAlarmTrend(params);

		return R.ok(list);
	}


	/**
	 * 查询历史告警
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "二级-首页中间地图-下钻-底部历史告警数据列表", notes = "二级-首页中间地图-下钻-底部历史告警数据列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaCode", value = "区域编码", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/analysis/queryTodayHistoryData")
	public R<Object> queryTodayHistoryData(@RequestBody AlarmListParams params) {
		PageDataInfo pageDataInfo = areaAlarmAnalysisService.queryHistoryData(params);
		return R.ok(pageDataInfo);
	}



	/**
	 * 告警分类统计导出Excel
	 *
	 * @param params 请求参数
	 * @return 告警分类统计数据
	 */
	@ApiOperation(value = "二级-首页中间地图-下钻-底部历史告警数据列表-下载", notes = "二级-首页中间地图-下钻-底部历史告警数据列表-下载")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaCode", value = "区域编码",	 paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间",  paramType = "query",   required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间",	 paramType = "query",   required = true, 	dataType = "String")
	})
	@PostMapping("/analysis/queryTodayHistoryDataExport")
	public void queryTodayHistoryDataExport(@RequestBody AlarmListParams params, HttpServletResponse response) throws ParseException {
		try {
			// 1、查询要导出的数据
			params.setPageNum(1);
			//查询es表
			params.setPageSize(-1);
			PageDataInfo pageDataInfo = BeanUtils.cast(queryTodayHistoryData(params).getData());
			List<AlarmHistDetailListVo> list = BeanUtils.cast(pageDataInfo.getData());
			// 初始化Excel序号
			int index = 1;
			for (AlarmHistDetailListVo vo : list) {
				vo.setFailRate(vo.getFailRate() == null ? "" : (vo.getFailRate() + "%"));
				vo.setHealthRate(vo.getHealthRate() == null ? "" : (vo.getHealthRate() + "%"));
				vo.setIndex(index++);
			}
			// 2、导出Excel

			//设置表头
			List<String> headParamsList = new ArrayList<>();
			String areaName = getArea(params.getAreaName());
			String title = "历史告警数据总览表";
			String fileName = title + System.currentTimeMillis();
			String sheetName = title;
			headParamsList.add(title);
			headParamsList.add(areaName);

			String formTitleName = ExcelTitleUtils.formTitleName(headParamsList, params.getStartTime(), params.getEndTime());
			List<List<String>> headList = new ArrayList<>();
			List<String> titleList = Arrays.asList("序号", "告警时间", "总访问量", "告警访问量", "响应慢访问量", "失败访问量", "失败率", "健康度");
			titleList.forEach(s -> {
				List<String> childHeadList = new ArrayList<>();
				childHeadList.add(formTitleName);
				childHeadList.add(s);
				headList.add(childHeadList);
			});

			ExcelUtil.writeExcel(AlarmHistDetailListVo.class, fileName, sheetName, list, headList, response);

		} catch (Exception e) {
			e.printStackTrace();
//			ResponseUtil.setResponseResult(Result
//					.setFail(ApiResultCode.EXPORT_EXCEL_ERROR.getCode(), ApiResultCode.EXPORT_EXCEL_ERROR.getMsg()), response);
		}
	}


	/**
	 * 三级-地图下钻-历史告警列表下钻-区域告警分析头部总览信息
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-区域告警分析头部总览信息", notes = "三级-地图下钻-历史告警列表下钻-区域告警分析头部总览信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", 		  value = "告警ID",  paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "cycleTime", value = "时间周期", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/detail/queryById")
	public R<Object> queryById(@RequestBody AlarmListParams params) {
		AlarmAnalysisVo alarmAnalysisVo = areaAlarmAnalysisService.queryAlarmByArea(params);
		if (alarmAnalysisVo != null) {
			alarmAnalysisVo.setAlarmTypeStr("正常告警");
		}
		return R.ok(alarmAnalysisVo);
	}

	/**
	 * 区域-全系统总览
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-区域告警全系统总览列表", notes = "三级-地图下钻-历史告警列表下钻-区域告警全系统总览列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaCode",  value = "区域编码", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   value = "页码", 		paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize",  value = "每页大小", 	paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "cycleTime", value = "所属时间周期", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime",   value = "结束时间", 	paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/detail/systemOverview")
	public R<Object> systemStat(@RequestBody StatusAlarmStatParams params) {
		PageDataInfo pageDataInfo = areaAlarmAnalysisService.querySystemStatByArea(params);
		return R.ok(pageDataInfo);
	}


	/**
	 * 区域-全系统总览统计导出Excel
	 *
	 * @param params 请求参数
	 * @return 告警分类统计数据
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-区域告警全系统总览列表-下载Excel", notes = "三级-地图下钻-历史告警列表下钻-区域告警全系统总览列表-下载Excel")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "areaCode",  value = "区域编码", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaName",  value = "区域名称", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   value = "页码", 		paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize",  value = "每页大小", 	paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "cycleTime", value = "所属时间周期", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime",   value = "结束时间", 	paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/detail/systemOverviewExport")
	public void systemOverviewExport(@RequestBody StatusAlarmStatParams params, HttpServletResponse response) throws ParseException {
		try {
			// 1、查询要导出的数据
			params.setPageNum(1);
			params.setPageSize(1000000L);
			PageDataInfo pageDataInfo = BeanUtils.cast(systemStat(params).getData());
			List<BusiStatusAlarmStatVo> list = BeanUtils.cast(pageDataInfo.getData());
			List<BusiStatusAlarmStatSystemExcelVo> resList = new ArrayList<>();
			resList = BeanUtils.convert(list, BusiStatusAlarmStatSystemExcelVo.class);
			// 初始化Excel序号
			int index = 1;
			for (BusiStatusAlarmStatSystemExcelVo vo : resList) {
				vo.setFailRateStr(vo.getFailRate() + "%");
				vo.setHealthRateStr(vo.getHealthRate() + "%");
				vo.setIndex(index++);
			}
			// 2、导出Excel
			//设置表头
			List<String> headParamsList = new ArrayList<>();
			String areaName = getArea(params.getAreaName());
			String alarmIndicatorName = "健康度低告警详情分析";
			String title = "全系统数据总览表";
			String fileName = title + System.currentTimeMillis();
			headParamsList.add(title);
			headParamsList.add(areaName);
			headParamsList.add(alarmIndicatorName);
			String formTitleName = ExcelTitleUtils.formTitleName(headParamsList, params.getCycleTime());
			List<List<String>> headList = new ArrayList<>();
			List<String> titleList = Arrays.asList("序号", "系统名称", "访问量", "受理量", "失败数", "告警数", "失败率", "健康率");
			titleList.forEach(s -> {
				List<String> childHeadList = new ArrayList<>();
				childHeadList.add(formTitleName);
				childHeadList.add(s);
				headList.add(childHeadList);
			});

			ExcelUtil.writeExcel(BusiStatusAlarmStatSystemExcelVo.class, fileName, title, resList, headList, response);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 区域-系统-业务总览
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-区域告警全业务总览列表", notes = "三级-地图下钻-历史告警列表下钻-区域告警全业务总览列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  value = "系统id",  	paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "areaCode",  value = "区域编码", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   value = "页码", 		paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize",  value = "每页大小", 	paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "cycleTime", value = "所属时间周期", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/detail/businessOverview")
	public R<Object> businessStat(@RequestBody StatusAlarmStatParams params) {
		PageDataInfo pageDataInfo = areaAlarmAnalysisService.queryDataBySystem(params);
		return R.ok(pageDataInfo);
	}


	/**
	 * 业区域-系统-业务总览导出Excel
	 *
	 * @param params 请求参数
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-区域告警全业务总览列表-下载Excel", notes = "三级-地图下钻-历史告警列表下钻-区域告警全业务总览列表-下载Excel")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  value = "系统id",  	paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "areaCode",  value = "区域编码", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaName",  value = "区域名称", 	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   value = "页码", 		paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize",  value = "每页大小", 	paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "cycleTime", value = "所属时间周期", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/detail/businessOverviewExport")
	public void businessOverviewExport(@RequestBody StatusAlarmStatParams params, HttpServletResponse response) throws ParseException {
		try {
			// 1、查询要导出的数据
			params.setPageNum(1);
			params.setPageSize(1000000L);
			params.setStartTime(null);
			params.setEndTime(null);
			PageDataInfo pageDataInfo = BeanUtils.cast(businessStat(params).getData());
			List<BusiStatusAlarmStatVo> list = BeanUtils.cast(pageDataInfo.getData());
			List<BusiStatusAlarmStatBusinessExcelVo> resList = new ArrayList<>();
			resList = BeanUtils.convert(list, BusiStatusAlarmStatBusinessExcelVo.class);
			// 初始化Excel序号
			int index = 1;
			for (BusiStatusAlarmStatBusinessExcelVo vo : resList) {
				vo.setFailRateStr(vo.getFailRate() + "%");
				vo.setHealthRateStr(vo.getHealthRate() + "%");
				vo.setIndex(index++);
			}
			// 2、导出Excel
			//设置表头
			List<String> headParamsList = new ArrayList<>();
			String areaName = getArea(params.getAreaName());
			String systemName = getSystemName(params.getSystemName());
			String alarmIndicatorName = "健康度低告警详情分析";
			String title = "全业务数据总览表";

			headParamsList.add(title);
			headParamsList.add(areaName);
			headParamsList.add(systemName);
			headParamsList.add(alarmIndicatorName);

			String fileName = title + System.currentTimeMillis();
			String formTitleName = ExcelTitleUtils.formTitleName(headParamsList, params.getCycleTime());
			List<List<String>> headList = new ArrayList<>();
			List<String> titleList = Arrays.asList("序号", "系统名称", "业务名称", "访问量", "受理量", "失败数", "告警数", "失败率", "健康率");
			titleList.forEach(s -> {
				List<String> childHeadList = new ArrayList<>();
				childHeadList.add(formTitleName);
				childHeadList.add(s);
				headList.add(childHeadList);
			});

			ExcelUtil.writeExcel(BusiStatusAlarmStatBusinessExcelVo.class, fileName, title, resList, headList, response);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 运维工作台 -> 首页地图下钻 -> 历史告警列表 -> 告警分析 -> 失败数、告警数下钻详单列表
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-失败数、告警数下钻详单列表", notes = "三级-地图下钻-历史告警列表下钻-失败数、告警数下钻详单列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  		value = "系统id",  		paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "businessCode",  	value = "系统编码",  	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "visitCountType",  value = "访问量类型",  	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaCode",  		value = "区域编码", 		paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   		value = "页码", 			paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 		value = "每页大小", 		paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "cycleTime", 		value = "所属时间周期",   paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/detail/alarmTypeToBusinessList")
	public R<Object> alarmTypeToBusinessList(@RequestBody StatusAlarmStatParams params) throws ParseException {

		return R.ok(areaAlarmAnalysisService.alarmTypeToBusinessList(params));
	}

	/**
	 * 运维工作台 -> 首页地图下钻 -> 历史告警列表 -> 告警分析 -> 失败数、告警数下钻详单列表
	 *
	 * @param params 请求对象
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-失败数、告警数下钻详单列表-下载Excel", notes = "三级-地图下钻-历史告警列表下钻-失败数、告警数下钻详单列表-下载Excel")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  		value = "系统id",  		paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "businessCode",  	value = "系统编码",  	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "visitCountType",  value = "访问量类型",  	paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaCode",  		value = "区域编码", 		paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaName",  		value = "区域名称", 		paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "cycleTime", 		value = "所属时间周期",   paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/detail/alarmTypeToBusinessListExport")
	public void alarmTypeToBusinessListExport(@RequestBody StatusAlarmStatParams params, HttpServletResponse response) throws ParseException, IOException {
		params.setPageNum(1);
		params.setPageSize(-1);
		PageDataInfo pageDataInfo = areaAlarmAnalysisService.alarmTypeToBusinessList(params);
		List<BusinessDetailedListVo> list = BeanUtils.cast(pageDataInfo.getData());

		String title = "告警指标数据总览表";

		if (StringUtils.isNotBlank(params.getAreaName())
				&&StringUtils.isNotBlank(params.getSystemName())
				&&StringUtils.isNotBlank(params.getBusinessName())
				&&StringUtils.isNotBlank(params.getVisitCountTypeName())) {

			title=params.getAreaName()+"区域"+params.getSystemName()+"系统"+params.getBusinessName()+"业务"+params.getVisitCountTypeName()+title;
		}

		String fileName = title + System.currentTimeMillis();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date cycleDate= DateUtils.parseDate(params.getCycleTime(), "yyyy-MM-dd HH:mm:ss");
		String startTimeStr = DateUtil.formatDateTime(YDateUtil.getBeforeMinute(cycleDate, 5));
		params.setEndTime(params.getCycleTime());
		params.setStartTime(startTimeStr);
		String formTitleName = title + simpleDateFormat.format(simpleDateFormat.parse(params.getStartTime())) + "~"
				+ simpleDateFormat.format(simpleDateFormat.parse(params.getEndTime()));

		List<List<String>> headList = new ArrayList<>();
		List<String> titleList = Arrays.asList("序号","业务名称", "自定义返回码", "用户标识", "开始时间", "结束时间",
				"业务状态", "完整性","办理时长","客户端空闲时长","告警数");
		titleList.forEach(s -> {
			List<String> childHeadList = new ArrayList<>();
			childHeadList.add(formTitleName);
			childHeadList.add(s);
			headList.add(childHeadList);
		});

		ExcelUtil.writeExcel(BusinessDetailedListVo.class, fileName, title, list, headList,20, response);
	}


	/**
	 * 三级-地图下钻-历史告警列表下钻-关联告警列表
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-关联告警列表", notes = "三级-地图下钻-历史告警列表下钻-关联告警列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "businessCode",  		value = "系统编码",  		paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "alarmIndicatorCodes", value = "告警指标编码集合",  	paramType = "query", required = true, dataType = "List<String>"),
			@ApiImplicitParam(name = "areaCode",  			value = "区域编码", 			paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 			paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("alarmDetailListQuery")
	public R<Object> alarmDetailListQuery(@RequestBody AlarmListParams params){
		PageDataInfo data = areaAlarmAnalysisService.alarmDetailListQuery(params);
		return R.ok(data);
	}

	/**
	 * 三级-地图下钻-历史告警列表下钻-关联告警列表
	 *
	 * @param params 请求对象
	 */
	@ApiOperation(value = "三级-地图下钻-历史告警列表下钻-关联告警列表-下载Excel", notes = "三级-地图下钻-历史告警列表下钻-关联告警列表-下载Excel")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  				paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "systemName",  		value = "系统名称",  			paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "businessCode",  		value = "系统编码",  			paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "businessName",  		value = "业务名称",  			paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "alarmIndicatorCodes", value = "告警指标编码集合",  		paramType = "query", required = true, dataType = "List<String>"),
			@ApiImplicitParam(name = "alarmIndicatorName",  value = "告警指标名称",  			paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaCode",  			value = "区域编码", 				paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "areaName",  			value = "区域名称", 				paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 				paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 				paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("alarmDetailListQueryExport")
	public void alarmDetailListQueryExport(@RequestBody AlarmListParams params, HttpServletResponse response) throws IOException, ParseException {
		params.setPageNum(1);
		params.setPageSize(-1);
		PageDataInfo pageDataInfo = BeanUtils.cast(alarmDetailListQuery(params).getData());
		List<AlarmDetailListQueryVo> list = BeanUtils.cast(pageDataInfo.getData());

		String title = "告警指标数据总览表";
		if (StringUtils.isNotBlank(params.getAreaName())
				&&StringUtils.isNotBlank(params.getSystemName())
				&&StringUtils.isNotBlank(params.getBusinessName())){
			if (params.getAlarmIndicatorCodes()!=null&&params.getAlarmIndicatorCodes().size()==1){
				if(("BS_FAIL_RATE_10").equals(params.getAlarmIndicatorCodes().get(0))){
					title= "失败率高" + title + "-" + params.getAreaName() + "区域-失败率高告警详情分析-" + params.getSystemName() +"系统-"+params.getBusinessName()+"业务";
				}else if (("BS_FAIL_RATE_20").equals(params.getAlarmIndicatorCodes().get(0))){
					title= "响应慢" + title + "-" + params.getAreaName() + "区域-响应慢告警详情分析-" + params.getSystemName() +"系统-"+params.getBusinessName()+"业务";
				}else if (("BS_FAIL_RATE_30").equals(params.getAlarmIndicatorCodes().get(0))){
					title= "健康度低" + title + "-" + params.getAreaName() + "区域-健康度低告警详情分析-" + params.getSystemName() +"系统-"+params.getBusinessName()+"业务";
				}
			}else{
				title= "所有" + title + "-" + params.getAreaName() + "区域-所有告警详情分析-" + params.getSystemName()+"系统-"+params.getBusinessName()+"业务";
			}
		}

		String sheetName = title;

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String formTitleName = title + Constants.DOUBLE_SPACE + simpleDateFormat.format(simpleDateFormat.parse(params.getStartTime())) + "~"
				+ simpleDateFormat.format(simpleDateFormat.parse(params.getEndTime()));

		List<List<String>> headList = new ArrayList<>();
		List<String> titleList = Arrays.asList("序号", "告警时间", "总访问量", "告警访问量", "响应慢访问量", "失败访问量", "告警指标", "告警值");
		titleList.forEach(s -> {
			List<String> childHeadList = new ArrayList<>();
			childHeadList.add(formTitleName);
			childHeadList.add(s);
			headList.add(childHeadList);
		});

		ExcelUtil.writeExcel(AlarmDetailListQueryVo.class, "告警指标数据总览表", sheetName, list, headList, response);
	}

	/**
	 * 格式化区域名称
	 * @param areaName 区域名称
	 * @return 区域
	 */
	public static String getArea(String areaName) {
		return areaName == null ? "未知区域" : (areaName + "区域");
	}


	/**
	 * 格式化系统名称
	 * @param systemName 系统名称
	 * @return 系统名称
	 */
	public static String getSystemName(String systemName) {
		return systemName == null ? "未知系统" : (systemName + "系统");
	}
}
